'''
Created on 2012-4-5

@author: qianwp
'''
from functools import wraps
from popcan.biz.accessors import CustomerAccessor,SessionAccessor
from popcan.helper.requesthelper import respError

def require_login(func):
    @wraps(func)
    def new_func(*args,**kwargs):
        request = args[0]
        session_id = request.COOKIES.get('session_id',None)
        if session_id :
            session = SessionAccessor().getSession(session_id)
            if session:
                request.affix['session'] = session
                return func(*args,**kwargs)
        return respError('sessionid_empty','you must login first')
    return new_func

def require_designer(func):
    @wraps(func)
    def new_func(*args,**kwargs):
        request = args[0]
        session = request.affix['session']
        customer = CustomerAccessor().getCustomer(session.custId)
        if not customer:
            return respError('customer_notfound','you must be a valid customer first')
        if customer.upgraded:
            request.affix['customer'] = customer
            return func(*args,**kwargs)
        return respError('not_designer','you should be a designer')
    return new_func
